﻿window.SearchKey = null;
$.MGJ.Dealer = [];
(function () {
    var _this = $.MGJ.Dealer;
    $.extend($.MGJ.Dealer, {
        //经销商导出
        ExportDealer: function (ele) {
            $("input.AllClickChild:checked").each(function () {
                info.push($(this).attr('ID'));
            });
            if (info.length == 0) {
                $.L.msgError("请选择要导出的经销商！");
                return
            }

            if (!$(ele).find('.no-result').hasClass('no-result')) {
                window.open(ACTION_URL("ExportDealer", "Manager") + "?Key=" + info);
            }
            else {
                $.L.msgWarning('导出数据不能为空！')
            }
        },
        //经销商导入
        ImportDealer: function () {
            $.L.dialog({
                url: action_url("Dealer", "Import"),
                title: "经销商信息",
                width: 400,
                height: 'auto',
                success: function (response) {
                   // if (response) $.L.msgError("经销商：" + response + "导入失败！");
                    // else $.L.msgSuccess("导入成功！");
                    $.L.msgSuccess(response);
                }
            })
        }
    });
})(jQuery);

//弹出窗
(function () {
    var _this = $.MGJ.Dealer;
    $.extend($.MGJ.Dealer, {
        // 新增或编辑
        Dialog_AEditObj: null,
        Dialog_AEdit: function (obj) {
            var id = $(obj).attr("ID");
            this.Dialog_AEditObj = $.L.dialog({
                url: ACTION_URL("AEdit", "Manager") + "?id=" + id,
                title: "经销商信息",
                width: 800,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#form").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_SaveDate();
                                //$(".btn-primary").attr("disabled", true);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

        },
        // 显示详细数据
        Dialog_ShowDataObj: null,
        Dialog_ShowData: function (obj) {
            var id = $(obj).attr("ID");
            this.Dialog_ShowDataObj = $.L.dialog({
                url: ACTION_URL("DealerInfo", "Manager") + "?id=" + id,
                title: "详细数据",
                width: 1100,
                height: 'auto',
                buttons: {
                    "button_cancel": {
                        type: 'primary',
                        text: '返回',
                        click: 'MODALMISS'
                    }
                },
            });
        },
    });
})(jQuery);

//AJax
(function () {
    var _index = 1;
    var _this = $.MGJ.Dealer;
    $.extend($.MGJ.Dealer, {
        //分页查询
        Ajax_PageList: function (pageIndex, Eid) {
            debugger
            Eid = Eid || 0;
            _index = pageIndex;
            window.SearchKey = removeSpace($("#Key").val());
            $.L.ajaxHandle({
                url: ACTION_URL("DiscPageList", "Manager"),
                data: {
                    key: window.SearchKey,
                    eId:Eid,
                    pageIndex: pageIndex,
                    pageSize: 10
                },
                success: function (response) {
                    $("#tableList").L_bindingDataList(response);
                    $("#tableList_left").L_bindingDataList(response);
                    $("#tableList_right").L_bindingDataList(response);
                }
            });
        },
        //保存
        Ajax_SaveDate: function () {
            if ($("#JurisdictionPrivance").val()==null)
            {
                $.L.msgError("请选择管辖区域");
                return;
            }
            for (var i = 0; i < 6; i++) {
                var string=$(".filter-option.pull-left").eq(i).text();
                if (string.indexOf("请选择")!=-1) {
                    $(".filter-option.pull-left").eq(i).text("");
                }
            }
            var form = $("#form").serialize();
            var phoneNumber = $("#ContactMobile").val();
            form += "&JurisdictionPrivanceName=" + $(".filter-option.pull-left").eq(0).text() + "&JurisdictionCityName=" + $(".filter-option.pull-left").eq(1).text() + "&JurisdictionRegionName=" + $(".filter-option.pull-left").eq(2).text()
            + "&AcrossPrivanceName=" + $(".filter-option.pull-left").eq(3).text() + "&AcrossCityName=" + $(".filter-option.pull-left").eq(4).text() + "&AcrossRegionName=" + $(".filter-option.pull-left").eq(5).text()
          + "&JurisdictionPrivance=" + $("#JurisdictionPrivance").val() + "&JurisdictionCity=" + $("#JurisdictionCity").val() + "&JurisdictionRegion=" + $("#JurisdictionRegion").val()
            + "&AcrossPrivance=" + $("#AcrossPrivance").val() + "&AcrossCity=" + $("#AcrossCity").val() + "&AcrossRegion=" + $("#AcrossRegion").val();
            $.L.ajaxHandle({
                url: ACTION_URL("DiscSaveDate", "Manager"),
                data: form,
                success: function (r) {
                    if (r <= 0) {
                        if (r == -1) {
                            $.L.msgError("经销商名称或联系电话系统已存在！");
                        }
                        else {
                            $.L.msgError("提交失败！");
                        }
                    }

                    else {
                        $.L.msgSuccess("提交成功！");

                        _this.Ajax_PageList(1);
                        _this.Dialog_AEditObj.close();
                    }

                }
            });
        },
        //删除
        Ajax_Del: function (obj) {
            $.L.msgConfirm("确认删除该记录?", function () {
                var id = $(obj).attr("ID");
                $.L.ajaxHandle({
                    url: ACTION_URL("DiscDel", "Manager"),//+ "?id=" + id,
                    data: {
                        id: id,
                    },
                    success: function (r) {
                        if (r <= 0) $.L.msgSuccess("删除失败！");

                        $.L.msgSuccess("删除成功！");
                        _this.Ajax_PageList(_index);
                    }
                });
            })

        },
        //下载模板
        ImportDemo: function () {
            window.open("/Manager/ExportDemo");
        }
    });
})(jQuery);
//回调函数
(function () {
    var _this = $.MGJ.Dealer;
    $.extend($.MGJ.Dealer, {

    });
})(jQuery);


$(document).on("change", ".AllClickChild", function () {
    var infoID = $(this).attr("id");
    if (this.checked) {
        for (var i = 0; i < info.length; i++) {
            if (info[i] == infoID) return;
        }
        info.push(infoID)
    } else {
        for (var i = 0; i < info.length; i++) {
            if (info[i] == infoID) {
                info.splice(i, 1);
                return;
            }
        }
    }
})